import { http } from "../../utils"

export const getChannel = () => {
  return async (dispatch) => {
    const res = await http.get("/channels")
    const { data, message } = res.data
    if (message === "OK") {
      dispatch({ type: "setChannels", payload: data.channels })
    }
  }
}
// 获取文章列表
export const getArticles = (params) => {
  return async (dispatch) => {
    const res = await http.get("/mp/articles", { params })
    const { data, message } = res.data
    if (message === "OK") {
      dispatch({
        type: "getArticles",
        payload: {
          page: data.page,
          pageSize: data.per_page,
          list: data.results.map((item, index) => {
            return {
              ...item,
              key: index,
              cover: item.cover.images[0]
            }
          }),
          count: data.total_count
        }
      })
    }
  }
}

// 删除文章
export const delArticle = (id) => {
  return async () => {
    await http.delete(`/mp/articles/${id}`)
  }
}

// 发表文章
export const publishSrticle = (params) => {
  // console.log(params)
  return async () => {
    await http.post(`/mp/articles`, params)
  }
}
// 获取文章详情
export const getArticlesDetail = (id) => {
  // console.log(params)
  return async () => {
    const { data } = await http.get(`/mp/articles/${id}`)
    return data
  }
}
// 编辑文章
export const editArticles = (params) => {
  // console.log(params)
  return async () => {
    const { data } = await http.put(`/mp/articles/${params.id}`, params)
    console.log(data)
  }
}
